package com.kevin.Code.Math;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @author Vinlee Xiao
 * @Classname PlusOne
 * @Description Leetcode 66 加一  简单题
 * @Date 2021/10/29 9:20
 * @Version 1.0
 */
public class PlusOne {


    /**
     * @param digits
     * @return
     */
    public int[] plusOne(int[] digits) {

        int len = digits.length;


        //表示进位的数字
        int carry = 0;
//        int reminder = 0;
        for (int i = len - 1; i >= 0; i--) {

            carry = (digits[i] + 1) / 10;
            digits[i] = (digits[i] + 1) % 10;

            if (carry == 0) {
                break;
            }
        }

        //如果最高位产生进位 则数组增加
//        System.out.println(carry);
        if (carry == 1) {
            int[] nums = new int[len + 1];
            nums[0] = carry;
            for (int i = 0; i < len; i++) {
                nums[i + 1] = digits[i];
            }
            return nums;
        }


        return digits;
    }

    public static void main(String[] args) {
        PlusOne plusOne = new PlusOne();
        int[] nums = new int[]{9};
        int[] arr = plusOne.plusOne(nums);

        for (int i : arr) {
            System.out.print(i + " ");

        }
    }
}
